package com.platform.common.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import com.aspose.cad.Color;
import com.aspose.cad.Image;
import com.aspose.cad.SmoothingMode;
import com.aspose.cad.imageoptions.CadRasterizationOptions;
import com.aspose.cad.imageoptions.PngOptions;

public class CadUtils {

	
	private static int height = 800;
    private static int width = 1000;


    private static CadRasterizationOptions getDefaultOption(){
        CadRasterizationOptions rasterizationOptions = new CadRasterizationOptions();
        rasterizationOptions.setPageWidth(width);
        rasterizationOptions.setPageHeight(height);
        rasterizationOptions.setAutomaticLayoutsScaling(true);
        rasterizationOptions.setNoScaling (false);
        rasterizationOptions.setContentAsBitmap(true);
        rasterizationOptions.setLayouts(new String[] {"Model"});
        rasterizationOptions.setDrawType(1);
        rasterizationOptions.setBackgroundColor(Color.getBlack().Clone());

        // Set Graphics options
        rasterizationOptions.getGraphicsOptions().setSmoothingMode(SmoothingMode.HighSpeed);

        return rasterizationOptions;
    }
	
	/**
	 * dwg 转化成  png 缩略图
	 * @param is
	 * @return
	 */
	public static InputStream dwg2png(InputStream is) {
        Image cadImage = Image.load(is);
        CadRasterizationOptions rasterizationOptions = getDefaultOption();
        PngOptions options = new PngOptions();
        options.setVectorRasterizationOptions(rasterizationOptions);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        cadImage.save(baos, options);
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        return bais;
		
	}
}
